package com.dongdongshop.service.impl;



import com.dongdongshop.api.SystemApi;
import com.dongdongshop.domain.req.TbGoodsReq;
import com.dongdongshop.domain.res.TbGoodsRes;
import com.dongdongshop.dto.ItemCatDTO;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.model.TbGoods;
import com.dongdongshop.model.TbGoodsDesc;
import com.dongdongshop.service.TbGoodsService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author 13412
* @description 针对表【tb_goods(商品表，SPU表)】的数据库操作Service实现
* @createDate 2024-09-04 16:35:38
*/
@Service
public class TbGoodsServiceImpl implements TbGoodsService{

    @Autowired
    private TbGoodsMapper tbGoodsMapper;

    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;

    @Autowired
    private SystemApi systemApi;

    @Override
    @Transactional
    public void insert(TbGoodsReq tbGoodsReq) {
        TbGoods tbGoods = new TbGoods();
        BeanUtils.copyProperties(tbGoodsReq, tbGoods);
        tbGoodsMapper.insert(tbGoods);
        TbGoodsDesc tbGoodsDesc = new TbGoodsDesc();
        BeanUtils.copyProperties(tbGoodsReq.getDesc(), tbGoodsDesc);
        tbGoodsDesc.setGoodsId(tbGoods.getId());
        tbGoodsDescMapper.insert(tbGoodsDesc);
    }

    @Transactional
    @Override
    public List<TbGoodsRes> queryAll() {
        List<TbGoods> goodsList=tbGoodsMapper.queryAll();
        List<ItemCatDTO> itemList = systemApi.queryAll();
        return updateCategoryUtil(goodsList,itemList);
    }

    @Override
    public List<TbGoodsRes> queryBySellerId(String threadLocal) {
        List<TbGoods> goodsList= tbGoodsMapper.queryBySellerId(threadLocal);
        List<ItemCatDTO> itemList = systemApi.queryAll();
        return updateCategoryUtil(goodsList,itemList);
    }

    @Override
    public void updateAuditStatus(TbGoodsReq tbGoodsReq) {
        tbGoodsMapper.updateAuditStatus(tbGoodsReq);
    }


    public static List<TbGoodsRes> updateCategoryUtil(List<TbGoods> goodsList,List<ItemCatDTO> itemList){
        List<TbGoodsRes> collect = goodsList.stream().map(i -> {
            TbGoodsRes tbGoodsRes = new TbGoodsRes();
            BeanUtils.copyProperties(i, tbGoodsRes);
            for (ItemCatDTO itemCatDTO : itemList) {
                if (itemCatDTO.getId().equals(i.getCategory1Id())) {
                    tbGoodsRes.getCategoryNames().setCategory1(itemCatDTO.getName());
                } else if (itemCatDTO.getId().equals(i.getCategory2Id())) {
                    tbGoodsRes.getCategoryNames().setCategory2(itemCatDTO.getName());
                } else if (itemCatDTO.getId().equals(i.getCategory3Id())) {
                    tbGoodsRes.getCategoryNames().setCategory3(itemCatDTO.getName());
                }
            }
            return tbGoodsRes;
        }).collect(Collectors.toList());
        return collect;
    }
}




